<template>
    <div>
        <el-tabs type="card" v-model="editableTabsValue" closable @tab-remove="removeTab">
            <el-tab-pane name="条目历史">
                <span slot="label">
                    <i class="el-icon-discover"></i>
                    条目历史
                </span>
                <div class="table-content">
                    <ItemHistoryTable />
                </div>
            </el-tab-pane>
            <el-tab-pane v-for="item in editableTabs" :key="item.name" :label="item.title" :name="item.name">
                <!-- <ItemHistoryDetail :itemData="item" @opne-table="getInstruct" v-if="item.type == 'look'"></ItemHistoryDetail> -->
                <ItemControlDetail :itemData="item" @opne-table="getInstruct" :key="item.name" v-if="item.type == 'history-look'"></ItemControlDetail>
                <!-- <CustomSetting :itemData="item" @close-custom="openCustom" v-if="item.type == 'customize'"></CustomSetting>
        <ItemUpdateRecord :itemData="item" @close-custom="openCustom" v-if="item.type == 'record'"></ItemUpdateRecord> -->
            </el-tab-pane>
        </el-tabs>
    </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import ItemHistoryDetail from './components/item-history/item-history-detail.vue';
import ItemControlDetail from './components/item-setting/item-control-detail.vue';
import CustomSetting from './components/item-setting/custom-setting.vue';
import RiskEmitter from '@/common/mixis/risk-emitter.vue';
import { mixins } from 'vue-class-component';
import ItemUpdateRecord from './components/item-setting/item-update-record.vue';
import ItemHistoryTable from '../setting/components/item-setting/item-history-table.vue';

export interface pages {
    total?: number;
    pageSize?: number;
    currentPage?: number;
}
@Component({
    name: 'ItemHistory',
    components: {
        ItemHistoryTable,
        ItemControlDetail,
        ItemHistoryDetail,
        CustomSetting,
        ItemUpdateRecord,
    },
})
export default class ItemHistory extends mixins(RiskEmitter) {
    editableTabsValue: string = '条目历史';
    editableTabs: any = [];
    tabsIndex: any = 0;
    historyNo: string = '';
    created() {
        this.$on('addTables', this.changComponets);
    }
    // 监控子组件发送的命令
    changComponets(datas: any) {
        const data: any = {};
        data.type = datas.type;
        data.content = datas.entryCode;
        data.historyNo = datas.historyNo;
        if (datas.type === 'look') {
            data.title = '查看：' + datas.entryCode + ' - ' + datas.historyNo;
            data.name = '查看：' + datas.entryCode + ' - ' + datas.historyNo;
            data.type = 'history-look';
        }
        const item = this.editableTabs.find((item: any) => item.name === data.name);
        if (!item) {
            this.addTab(data);
        } else {
            this.editableTabsValue = data.name;
        }
    }
    // 传过来的关闭页面
    getInstruct(data: any) {
        this.tabsIndex = this.tabsIndex + 1;
        this.removeTab(data);
    }

    // 关闭tabs
    removeTab(targetName: string) {
        let tabs = this.editableTabs;
        let activeName = this.editableTabsValue;
        if (activeName === targetName) {
            tabs.forEach((tab: any, index: any) => {
                if (tab.name === targetName) {
                    let nextTab = tabs[index + 1] || tabs[index - 1];
                    if (nextTab) {
                        activeName = nextTab.name;
                    }
                }
            });
        }
        // 默认跳转到条目历史
        this.editableTabsValue = '条目历史';
        this.editableTabs = tabs.filter((tab: any) => tab.name !== targetName);
    }

    // 新增tabs
    addTab(targetName: any) {
        this.editableTabs.push(targetName);
        this.editableTabsValue = targetName.name;
    }

    // 关闭自定义
    openCustom(val: any) {
        this.editableTabsValue = '条目历史';
        this.removeTab(val);
    }
}
</script>
<style lang="scss" scoped>
/deep/ .el-tabs .el-tabs__nav .el-tabs__item:nth-child(1) .el-icon-close {
    display: none;
}
/deep/ .el-tabs {
    height: 100%;
    .el-tabs__content {
        height: calc(100% - 40px);
        .el-tab-pane {
            height: 100%;
        }
    }
}
/deep/ .el-card {
    height: 100%;
    .el-card__body {
        height: 100%;
    }
}
</style>
